package com.fau.mm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.mm.action.PublicAction;
import com.fau.mm.entity.MedicalAdmin;
import com.fau.mm.entity.MedicalMediaPlacement;
import com.fau.mm.util.DateUtil;
import com.fau.mm.util.FileNameEncoderUtil;
import com.fau.mm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 媒体投放信息管理
 * @author dangjh
 * @datetime 2014年11月6日,上午10:43:52
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class MediaPlacementAction extends PublicAction implements ModelDriven<MedicalMediaPlacement> {

	private static final long serialVersionUID = -604751067777620012L;

	private MedicalMediaPlacement place = new MedicalMediaPlacement();
	
	/**
	 * @desc 实例化媒体投放信息列表页
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:22:19
	 */
	@Action(value = "init_media_place_list", results = {@Result(location = "init_media_place_list.jsp")})
	public String initMediaPlaceList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载实例化媒体投放信息数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:24:07
	 */
	@Action(value = "media_place_views")
	public String mediaPlaceViews(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getMediaService().findAllMediaPlacement(map, place, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 实例化媒体投放添加界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:40:19
	 */
	@Action(value = "medical_media_place_add", results = {@Result(location = "medical_media_place_add.jsp")})
	public String mediaPlaceAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存添加的媒体投放信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:41:42
	 */
	@Action(value = "medical_media_place_save")
	public String meidaPlaceSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getMediaService().savePlace(place);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 实例化媒体投放信息编辑界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:45:33
	 */
	@Action(value = "medical_media_place_edit", results = {@Result(location = "medical_media_place_edit.jsp")})
	public String mediaPlaceEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑的媒体投放信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:46:47
	 */
	@Action(value = "media_edit_place_save")
	public String mediaEditPlaceSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(place.getId())){
			boolean flag = this.baseAllService.getMediaService().saveEditPlace(place, admin);
			if(flag){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除媒体投放信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午1:49:45
	 */
	@Action(value = "medical_media_place_delete")
	public String mediaPlaceDelete(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(place.getId())){
			this.baseAllService.getMediaService().deletePlace(place);
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查看媒体投放信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月6日,下午5:05:15
	 */
	@Action(value = "medical_media_place_view", results = {@Result(location = "medical_media_place_view.jsp")})
	public String mediaPlaceView(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导出当前页面数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:51:07
	 */
	@Action(value = "export_current_media_place_data")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"媒体投放信息管理"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMediaService().currentPagePlace(ids);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出所有媒体投放信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月20日,上午11:51:49
	 */
	@Action(value = "export_all_media_place_data")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"媒体投放信息管理"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getMediaService().allPagePlace(user);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 加载所有产品信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月7日,下午5:50:59
	 */
	@Action(value = "medical_medicine_view")
	public String medicineView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMediaService().findAllMedicineByEid(admin.getEnterpriseId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 加载所有媒体信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月7日,下午5:52:03
	 */
	@Action(value = "medical_media_view_all")
	public String mediaInfoViewAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMediaService().findAllMediaByEid(admin.getEnterpriseId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 加载系统字典表--播放状态
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月8日,下午6:23:23
	 */
	@Action(value = "medical_mm_key_value_view")
	public String keyValueView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMediaService().getKeyValue(admin.getEnterpriseId(), SystemConfig.PLAYING_TYPE);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询公司外线号码
	 * @return null
	 * @author liy
	 * @datetime 2014年12月24日,下午2:02:34
	 */
	@Action(value = "medical_mm_outline")
	public String outlineList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getMediaService().findOutline();
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalMediaPlacement getModel() {
		return place;
	}

}
